package dao;

import util.DBHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDao {

    //查询该用户是否存在
   public Map<String,Object> userLogin(String name, String pwd, String status){
        String sql = "select * from shopping_user where uname = ? and pwd = ? and status = ? ";
        return DBHelper.selectOneMap(sql,name,pwd,status);
    }

    //查询所有的用户数据
    public long queryTotal(String name,String status){
       String sql = "select * from shopping_user where 1=1 ";
        ArrayList<Object> params = new ArrayList<>();
        if(name !=null && name.trim().isEmpty() == false){
           sql += " and uname like ? ";
           params.add("%" + name + "%");
       }
        if(status != null && status.trim().isEmpty() == false){
            sql += " and status like ? ";
            params.add("%" + status + "%");
        }
       return DBHelper.count(sql,params.toArray());
    }

    public Map<String,Object> userLogin(String name, String pwd){
        String sql = "select * from shopping_user where uname = ? and pwd = ?  ";
        return DBHelper.selectOneMap(sql,name,pwd);
    }



    //查询分页查询
    public List<Map<String,Object>> queryData(int begin,int size,String name,String status){
        String sql = "select * from shopping_user where 1=1 ";
        ArrayList<Object> params = new ArrayList<>();
        if(name !=null && name.trim().isEmpty() == false){
            sql += " and uname like ? ";
            params.add("%" + name + "%");
        }

        if(status != null && status.trim().isEmpty() == false){
            sql += " and status like ? ";
            params.add("%" + status + "%");
        }
        sql += "limit "+ begin + "," + size;
        return DBHelper.selectListMap(sql,params.toArray());
    }
    // 用户 添加
    public int saveUser(String uname, String pwd, String phone, String sex, String email, String image){
        String sql = "insert into shopping_user values(default, ?, ?, ?, ?, ?, 1, ?) ";
        return  DBHelper.update(sql, uname, pwd, phone, sex, email, image);
    }

    // 用户 修改
    public int modifyUser(String uname, String pwd, String phone, String sex, String email, String uid){
        String sql = "update shopping_user set uname = ?, pwd = ?, phone = ?, sex = ?, email = ? where uid = ? ";
        return DBHelper.update(sql, uname, pwd, phone, sex, email, uid);
    }

    //物理删除用户
    public int removeUser(String uid){
       String sql ="delete from shopping_user where uid =?";
       return DBHelper.update(sql,uid);
    }

    //逻辑删除用户
    public int modifyStatus(String uid,String status){
       String sql ="update shopping_user set status = ? where uid= ? ";
       return DBHelper.update(sql,status,uid);
    }

    //根据名字查询数据
    public Map<String, Object> findUser(String name){
       String sql = "select * from shopping_user where uname = ?";
       return DBHelper.selectOneMap(sql,name);
    }
    //根据用户id查询默认收获地址
    public Map<String, Object> findDefaultAddress(String uid){
            String sql = "select receiverprovince from shopping_user u inner join receshopping  s on u.uid = s.uid  where u.uid = ? and s.status='0' ";
        return DBHelper.selectOneMap(sql,uid);
    }

    //根据用户编号查询收货地址
    public List<Map<String, Object>> findAdress(String uid){
       String sql = "select r.*,uname from shopping_user u inner join receshopping r on u.uid = r.uid where u.uid = ? ";
       return DBHelper.selectListMap(sql,uid);
    }
}
